package service.contract.impl;

import java.util.List;
import model.TContract;
import model.contract.Contractensure;
import model.contract.Contractmortgage;
import model.contract.Contractpledge;
import model.contract.Loangrant;
import core.service.BaseService;
import service.contract.ShowContractService;
import dao.ContractensureDao;
import dao.ContractmortgageDao;
import dao.ContractpledgeDao;
import dao.LoangrantDao;
import dao.TContractDao;
import exception.Exceptions;

public class ShowContractServiceImpl extends BaseService implements ShowContractService {
	private TContractDao contractDao;
	private ContractensureDao ensureDao;
	private ContractmortgageDao mortgageDao;
	private ContractpledgeDao pledgeDao;
	private LoangrantDao loangrantDao;
	
 	@Override
	public List<?> get_htList(String str){
 		return contractDao.findModel(str,10);
	}
 	public List<TContract> get_htList(){
 		return contractDao.findAll();
 	}
 	public List<?> show_htList(String str){
 		String select=" contract.Id,contract.contractId,contract.clienttype," +
 				"(case  when contract.clienttype='1' then (select p.name from personalbasic as p where contract.clientid=p.id)"+
 				" when contract.clienttype='2' then (select c.name from companybasic as c where contract.clientid=c.id)"
 				+" else '0' end) as customname,"
 				+"contract.applymoney,contract.enddate";
		String from=" t_contract as contract"+str;
		return contractDao.findSqlQuery(select,from, 10);
 		
 	}

	public TContract get_ht(Integer htid) throws Exceptions{
		TContract ht=contractDao.findOne("id",htid);
		if(ht==null)throw new Exceptions("数据获取异常，数据库中没有此记录");
		return ht;
	}
	public TContract get_ht(String name,Integer value)throws Exceptions{
		TContract ht=contractDao.findOne(name, value);
		if(ht==null)throw new Exceptions("数据获取异常，请先生成借款合同");
		return ht;
	}
	public TContract get_ht(String name,String value)throws Exceptions{
		TContract ht=contractDao.findOne(name, value);
		if(ht==null)throw new Exceptions("数据获取异常，请先生成借款合同");
		return ht;
	}
	
	public List<?> get_htRelation(String name,String value,String type){
		if(type.equals("ensure"))
			return ensureDao.findList(name, value);
		else if(type.equals("mortgage"))
			return mortgageDao.findList(name, value);
		else if(type.equals("pledge"))
			return pledgeDao.findList(name, value);
		else
			return null;		
	}
	public Contractensure get_htEnsure(Integer id) throws Exceptions{
		Contractensure htensure=ensureDao.findOne("id", id);
		if(htensure==null)throw new Exceptions("数据获取异常，数据库中没有此记录");
		return htensure;
		
	}
	public Contractensure get_htEnsure(String name,Integer value)throws Exceptions{
		Contractensure htensure=ensureDao.findOne(name, value);
		if(htensure==null)throw new Exceptions("数据获取异常，数据库中没有此记录");
		return htensure;
	}
	public Contractensure get_htEnsure(String name,String value)throws Exceptions{
		Contractensure htensure=ensureDao.findOne(name, value);
		if(htensure==null)throw new Exceptions("数据获取异常，数据库中没有此记录");
		return htensure;
	}
	public List<Contractensure> get_htEnsure(){
		return ensureDao.findAll();
	}
	public Integer getCount(Integer type,String value){
		Integer countall=0;
		if(type==3){
		countall=ensureDao.countModel("contractloanid='"+value+"'");
		}
		if(type==1){
			countall=mortgageDao.countModel("contractloanid='"+value+"'");
		}
		if(type==2){
			countall=pledgeDao.countModel("contractloanid='"+value+"'");
		}
		return countall;
	}
	public Contractmortgage get_htMortgage(Integer id) throws Exceptions{
		Contractmortgage htmortgage=mortgageDao.findOne("id", id);
		if(htmortgage==null) throw new Exceptions("数据获取异常，数据库中没有此记录");
		return htmortgage;
	}
	public Contractmortgage get_htMortgage(String name,Integer value)throws Exceptions{
		Contractmortgage htmortgage=mortgageDao.findOne(name, value);
		if(htmortgage==null)throw new Exceptions("数据获取异常，数据库中没有此记录");
		return htmortgage;
	}
	public Contractmortgage get_htMortgage(String name,String value)throws Exceptions{
		Contractmortgage htmortgage=mortgageDao.findOne(name, value);
		if(htmortgage==null)throw new Exceptions("数据获取异常，数据库中没有此记录");
		return htmortgage;
	}
	public List<Contractmortgage> get_htMortgage(){
		return mortgageDao.findAll();
	}
	public Contractpledge get_htPledge(Integer id) throws Exceptions{
		Contractpledge htpledge=pledgeDao.findOne("id", id);
		if(htpledge==null)throw new Exceptions("数据获取异常，数据库中没有此记录");
		return htpledge;
	}
	public Contractpledge get_htPledge(String name,Integer value)throws Exceptions{
		Contractpledge htpledge=pledgeDao.findOne(name, value);
		if(htpledge==null)throw new Exceptions("数据获取异常，数据库中没有此记录");
		return htpledge;
	}
	public Contractpledge get_htPledge(String name,String value)throws Exceptions{
		Contractpledge htpledge=pledgeDao.findOne(name, value);
		if(htpledge==null)throw new Exceptions("数据获取异常，数据库中没有此记录");
		return htpledge;
	}
	public List<Contractpledge> get_htPledge(){
		return pledgeDao.findAll();
	}
	public List<Loangrant> get_loangrant(String str){
		return  loangrantDao.findModel(str);
	}

	//tttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt
	public TContractDao getContractDao() {
		return contractDao;
	}

	public void setContractDao(TContractDao contractDao) {
		this.contractDao = contractDao;
	}

	public ContractensureDao getEnsureDao() {
		return ensureDao;
	}

	public void setEnsureDao(ContractensureDao ensureDao) {
		this.ensureDao = ensureDao;
	}

	public ContractmortgageDao getMortgageDao() {
		return mortgageDao;
	}

	public void setMortgageDao(ContractmortgageDao mortgageDao) {
		this.mortgageDao = mortgageDao;
	}

	public ContractpledgeDao getPledgeDao() {
		return pledgeDao;
	}

	public void setPledgeDao(ContractpledgeDao pledgeDao) {
		this.pledgeDao = pledgeDao;
	}

	public LoangrantDao getLoangrantDao() {
		return loangrantDao;
	}

	public void setLoangrantDao(LoangrantDao loangrantDao) {
		this.loangrantDao = loangrantDao;
	}
	
	
}
